package com.sdry.system.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdry.basic.entity.Car;
import com.sdry.basic.entity.Driver;
import com.sdry.system.entity.PostUser;
import com.sdry.system.entity.RoleUser;
import com.sdry.system.entity.SysUser;
import com.sdry.system.mapper.UserMapper;
import com.sdry.untils.Md5Util;
import com.sdry.untils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;


/**
 * @version: 1.0
 * @description: 用户管理
 * @author: cb
 * @create: 2020-01-13 20:18
 **/
@Transactional(rollbackFor = Exception.class)
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,SysUser> implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 查询用户信息
     * @param username 用户名
     * @return
     */
    @Override
    public SysUser queryUserByUserName(String username) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username).eq("del_flag",0L);
        SysUser user = userMapper.selectOne(queryWrapper);
        return user;
    }

    /**
     * 添加用户
     * @param sysUser 用户对象
     * @param role 角色对象
     * @return
     */
    @Override
    public Integer addRole(SysUser sysUser, String[] role) {
        sysUser.setPassword(Md5Util.encrypt(sysUser.getPassword()));
        Integer affect = 0;
        try {
            sysUser.setCreateBy(UserContext.getSessionUserIdByServiceImpl().toString());
            userMapper.insert(sysUser);
            if (role != null) {
                for (String roleId : role) {
                    affect = userMapper.addRoleUser(roleId,sysUser.getUserId());
                }
            }
        }catch (Exception e){
        }

        return affect;
    }

    /**
     * 根据用户ID查询用户角色
     * @param userId 用户ID
     * @return
     */
    @Override
    public List<RoleUser> queryRoleUserByUserIdList(Long userId) {
        return userMapper.queryRoleUserByUserIdList(userId);
    }

    /**
     * 添加员工
     * @param sysUser
     * @param postId
     * @return
     */
    @Override
    public Integer addStaff(SysUser sysUser, String[] postId) {
        Integer affect = 0;
        try {
            sysUser.setCreateBy(UserContext.getSessionUserNameByServiceImpl());
            userMapper.insert(sysUser);
            if (postId != null) {
                for (String pId : postId) {
                    affect = userMapper.addPostUser(pId,sysUser.getUserId());
                }
            }
        }catch (Exception e){
        }
        return affect;
    }

    @Override
    public List<PostUser> queryPostUser(Long userId) {
        return userMapper.queryPostUser(userId);
    }

    @Override
    public Integer updateStaff(SysUser sysUser, String[] postId) {
        Integer affect = 0;
        try {
            sysUser.setUpdateDate(new Timestamp(System.currentTimeMillis()));
            sysUser.setCreateBy(UserContext.getSessionUserNameByServiceImpl());
            sysUser.setUpdateBy(UserContext.getSessionUserNameByServiceImpl());
            userMapper.updateById(sysUser);
            if (postId != null) {
                 affect = userMapper.deletePostUser(sysUser.getUserId());
                for (String pId : postId) {
                    affect = userMapper.addPostUser(pId,sysUser.getUserId());
                }
            }
        }catch (Exception e){
        }
        return affect;
    }

    @Override
    public Integer deleteUser(Long userId) {
        return userMapper.updateUserStatus(userId);
    }

    @Override
    public Integer updateUser(SysUser sysUser, String[] role) {
        Integer affect = 0;
        try {
            //更新时间
            sysUser.setUpdateDate(new Timestamp(System.currentTimeMillis()));
            sysUser.setUpdateBy(UserContext.getSessionUserIdByServiceImpl().toString());
            userMapper.updateById(sysUser);
            affect = userMapper.deleteRoleUser(sysUser.getUserId());
            if (role != null) {
                for (String roleId : role) {
                    affect = userMapper.addRoleUser(roleId,sysUser.getUserId());
                }
            }
        }catch (Exception e){
        }

        return affect;
    }

    @Override
    public List<Driver> queryDriver() {
        return userMapper.queryDriver();
    }

    @Override
    public List<Car> queryCar() {
        return userMapper.queryCar();
    }

    @Override
    public Integer changeUserPaw(Long userId, String oldPad, String newPad) {
        oldPad = Md5Util.encrypt(oldPad);
        newPad = Md5Util.encrypt(newPad);
        //1.新旧密码相同
        if (oldPad.equals(newPad)) {
            return 3;
        }
        //2.判断用户名和密码是否正确
        SysUser sysUser = userMapper.selectById(userId);
        if (!sysUser.getPassword().equals(oldPad)) {
             return 0;
        }
        Integer effect = userMapper.updatePaw(userId,newPad);
        return effect;
    }
}
